/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.gui.internalframe;

import edu.hpvas.control.OwnerController;
import edu.hpvas.control.Restrictions;
import edu.hpvas.persistence.entity.Owner;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author sony
 */
public class OwnerReadFrame extends javax.swing.JInternalFrame {
    
    OwnerController ownerC= new OwnerController();
    Restrictions restric = new Restrictions();
    ArrayList<Owner> owners = new ArrayList<>();
    Owner owner = new Owner();
    public OwnerReadFrame() {
        initComponents();
        int ancho=780;
         int alto=550;
         setSize(ancho, alto);
        initInfoOwner();
    }
    
    public void initInfoOwner(){
        nameJT.setVisible(false);
        nameJL.setVisible(false);
        birthPlaceJT.setVisible(false);
        lastnameJL.setVisible(false);
        birthDateJL.setVisible(false);
        birthDateJT.setVisible(false);
        birthPlaceJL.setVisible(false);
        lastNameJT.setVisible(false);
        ssNJT.setVisible(false);
        ssnJL.setVisible(false);
        petsInfJB.setVisible(false);
        selectJB.setVisible(false);
        nameJCB.setVisible(false);
        errorJL.setVisible(false);
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        nameJTF = new javax.swing.JTextField();
        nameJCB = new javax.swing.JComboBox();
        selectJB = new javax.swing.JButton();
        cancelJB = new javax.swing.JButton();
        errorJL = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        nameJT = new javax.swing.JTextField();
        birthPlaceJT = new javax.swing.JTextField();
        lastNameJT = new javax.swing.JTextField();
        birthDateJT = new javax.swing.JTextField();
        nameJL = new javax.swing.JLabel();
        lastnameJL = new javax.swing.JLabel();
        birthPlaceJL = new javax.swing.JLabel();
        birthDateJL = new javax.swing.JLabel();
        ssnJL = new javax.swing.JLabel();
        ssNJT = new javax.swing.JTextField();
        petsInfJB = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Owner Information\n", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 153)));

        jLabel1.setText("Search by name & Last Name");

        nameJTF.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nameJTFMouseClicked(evt);
            }
        });

        selectJB.setText("Select");
        selectJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectJBActionPerformed(evt);
            }
        });

        cancelJB.setText("Cancel");
        cancelJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJBActionPerformed(evt);
            }
        });

        errorJL.setForeground(new java.awt.Color(204, 0, 0));
        errorJL.setText("The special caracters are not allowed... try again");

        jButton1.setText("Enter");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(errorJL, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(nameJTF, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(nameJCB, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(selectJB, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(101, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancelJB)
                .addGap(21, 21, 21))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameJTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(8, 8, 8)
                .addComponent(errorJL)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameJCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectJB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancelJB)
                .addGap(46, 46, 46))
        );

        nameJT.setEditable(false);

        birthPlaceJT.setEditable(false);

        lastNameJT.setEditable(false);

        birthDateJT.setEditable(false);

        nameJL.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        nameJL.setText("NAME");

        lastnameJL.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lastnameJL.setText("LASTNAME");

        birthPlaceJL.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        birthPlaceJL.setText("BIRTH PLACE");

        birthDateJL.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        birthDateJL.setText("BIRTH DATE");

        ssnJL.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        ssnJL.setText("SSN");

        ssNJT.setEditable(false);

        petsInfJB.setText("Pets information");
        petsInfJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                petsInfJBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(birthPlaceJT)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(ssNJT))
                            .addComponent(nameJT)
                            .addComponent(lastNameJT)
                            .addComponent(birthDateJT))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(ssnJL)
                                .addGap(131, 131, 131)
                                .addComponent(petsInfJB))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(birthDateJL)
                                    .addComponent(lastnameJL)
                                    .addComponent(birthPlaceJL)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(nameJL)))
                        .addGap(48, 48, 48)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameJL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastnameJL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(birthDateJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(birthDateJL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(birthPlaceJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(birthPlaceJL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ssNJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ssnJL)
                    .addComponent(petsInfJB))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nameJTFMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nameJTFMouseClicked

        if(nameJTF.getText().equals("The field Cannot be Blank")){
            nameJTF.setText("");
            nameJTF.setForeground(Color.black);
        }
    }//GEN-LAST:event_nameJTFMouseClicked

    private void selectJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectJBActionPerformed
        if(nameJCB.getSelectedIndex()!=0){
            
            owner= owners.get(nameJCB.getSelectedIndex()-1);
            nameJTF.setText("");
            nameJCB.removeAll();
            nameJCB.setVisible(false);
            selectJB.setVisible(false);
            initOwnerInformation();

        }
    }//GEN-LAST:event_selectJBActionPerformed

    private void cancelJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJBActionPerformed
        this.setVisible(false);
        nameJTF.setText("");

    }//GEN-LAST:event_cancelJBActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        errorJL.setVisible(false);
        nameJCB.removeAllItems();
        if(nameJTF.getText().equals("") || nameJTF.getText().equals("The field Cannot be Blank")){
            nameJTF.setText("The field Cannot be Blank");
            nameJTF.setForeground(Color.red);
        }
        else if(!restric.validateSpecialC(nameJTF.getText())){
            errorJL.setVisible(true);
        }
        else{
            owners = ownerC.searchOwners(nameJTF.getText());
            if(!owners.isEmpty()){
                nameJCB.addItem("-----------");
                nameJCB.setVisible(true);
                selectJB.setVisible(true);
                for(Owner o:owners){
                    nameJCB.addItem(o.getIdPerson().getName() + " " + o.getIdPerson().getLastName());
                }

            }
            else{
                nameJCB.setVisible(false);
                selectJB.setVisible(false);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void petsInfJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_petsInfJBActionPerformed
        PetsInformation petsInfo = new PetsInformation();
        petsInfo.setOwner(owner);
        petsInfo.setVisible(true);
        
    }//GEN-LAST:event_petsInfJBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel birthDateJL;
    private javax.swing.JTextField birthDateJT;
    private javax.swing.JLabel birthPlaceJL;
    private javax.swing.JTextField birthPlaceJT;
    private javax.swing.JButton cancelJB;
    private javax.swing.JLabel errorJL;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField lastNameJT;
    private javax.swing.JLabel lastnameJL;
    private javax.swing.JComboBox nameJCB;
    private javax.swing.JLabel nameJL;
    private javax.swing.JTextField nameJT;
    private javax.swing.JTextField nameJTF;
    private javax.swing.JButton petsInfJB;
    private javax.swing.JButton selectJB;
    private javax.swing.JTextField ssNJT;
    private javax.swing.JLabel ssnJL;
    // End of variables declaration//GEN-END:variables

    private void initOwnerInformation() {
        nameJL.setVisible(true);
        nameJT.setText(owner.getIdPerson().getName());
        nameJT.setVisible(true);
        lastnameJL.setVisible(true);
        lastNameJT.setText(owner.getIdPerson().getLastName());
        lastNameJT.setVisible(true);
        birthDateJL.setVisible(true);
        birthDateJT.setText(owner.getIdPerson().getBirthdate().toString());
        birthDateJT.setVisible(true);
        birthPlaceJL.setVisible(true);
        birthPlaceJT.setText(owner.getIdPerson().getBirthPlace());
        birthPlaceJT.setVisible(true);
        ssnJL.setVisible(true);
        ssNJT.setVisible(true);
        ssNJT.setText(owner.getIdPerson().getSsn());
        petsInfJB.setVisible(true);
        
    }
}
